#####
# This script looks at correlates of social/political connectedness/density
#####

rm(list=ls())
packs = c('tidyverse', 'stringr', 'lmtest',
          'hrbrthemes', 'ggcorrplot')
source("repFile/r/helpers.R")
loadPkg(packs)

# load all data
load('repFile/data/combined-modeling-data.rda')



## look at correlations of neighborhood density and slum-level predictors
density = 
  df %>% 
  select(neigh_socVar, pol_lat_neigh, 
         slum_id = AA7, CC4, woman, CITY,
         frac_rel, frac_caste,
         JJ22, asset_sum) %>% 
  mutate(JJ22 = as.numeric(JJ22)) %>% 
  drop_na() %>% 
  group_by(slum_id) %>% 
  mutate(JJ22 = mean(JJ22), 
         asset_sum = mean(asset_sum), 
         woman = mean(woman), 
         CC4 = mean(CC4)) %>% 
  distinct(slum_id, .keep_all = T)


m1 = lm(neigh_socVar ~ frac_rel + frac_caste + JJ22 + asset_sum + woman + 
          CC4 + CITY, data = density)
m2 = lm(pol_lat_neigh ~  frac_rel + frac_caste + JJ22 + asset_sum + woman + 
          CC4 + CITY, data = density)
summary(m1)
summary(m2)

stargazer(m1, m2, 
          type = 'latex', 
          title = 'Correlates of neighborhood density. Models fit using OLS. One observation per slum.',
          keep.stat = c('n'), 
          intercept.bottom = F, 
          dep.var.labels = c('Social Density', 'Political Density'),
          covariate.labels = c('Intercept', 
                               'Religious frac.', 
                               'Caste frac.',
                               'Settlement recognition', 
                               'Average wealth index', 
                               'Percent women', 
                               'Average age', 
                               'Bangalore (2017)', 
                               'Jaipur', 
                               'Patna'), 
          label = 'density-corr', 
          out = 'repFile/paper/figures/density-corr.tex')



## look at correlations of individual level centrality
centrality = 
  df %>% 
  select(socVar, pol_lat, 
         slum_id = AA7, CC4, woman, CITY,
         muslim, christian, asset_sum) %>%
  drop_na()


m1 = lm(socVar ~ .-pol_lat-slum_id, data = centrality)
cls1 = coeftest(m1, vcovCluster(m1, m1$model$slum_id))

m2 = lm(pol_lat ~ .-socVar-slum_id, data = centrality)
cls2 = coeftest(m2, vcovCluster(m2, m2$model$slum_id))



stargazer(cls1, cls2, 
          type = 'latex', 
          title = 'Correlates of individual connectedness Models fit using OLS, standard errors clustered at slum.',
          intercept.bottom = F, 
          dep.var.labels.include = F,
          column.labels = c('Social Connectedness', 'Political Connectedness'),
          covariate.labels = c('Intercept', 
                               'Age', 
                               'Woman', 
                               'Bangalore (2017)', 
                               'Jaipur', 
                               'Patna',
                               'Muslim', 
                               'Christian', 
                               'Wealth index'), 
          label = 'centrality-corr', 
          out = 'repFile/paper/figures/centrality-corr.tex')



## does pol_lat_neigh predict slum recognition
m1 = glm(JJ22 ~ frac_rel + frac_caste + neigh_socVar + pol_lat_neigh
         + asset_sum + woman + 
           CC4 + CITY, data = density)
summary(m1)

stargazer(m1, 
          type = 'latex', 
          title = 'Correlates of formal slum recognition. Models fit using OLS.',
          intercept.bottom = F, 
          dep.var.labels.include = F,
          covariate.labels = c('Intercept', 
                               'Religious frac.', 
                               'Caste frac.',
                               'Neighborhood social density',
                               "Neighborhood political density",
                               'Average wealth index', 
                               'Percent women', 
                               'Average age', 
                               'Bangalore (2017)', 
                               'Jaipur', 
                               'Patna'), 
          label = 'pol-correlates', 
          out = 'repFile/paper/figures/pol-correlates.tex')